﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2011

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Navigation;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Applications.Web.RioWCFWorkflow;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.AppCode.ApplicationNavigation;
using Rio.Framework.Pages.ChildWindows;
using Rio.Framework.Pages.Context;

namespace Rio.Framework.Pages.Workflow.Actions
{
    [InheritedExport(typeof(WorkflowButtonAction))]
    public class ReplyWorkflowButtonAction : WorkflowButtonAction
    {
        #region Fields

        private long _folderTypeId;
        private long _organizationId;

        #endregion

        #region Constructors

        public ReplyWorkflowButtonAction()
        {
            Text = Resx.GetLabel(ResxWorkflow.WorkflowAction_Reply);
            Image = new Uri("/Rio.Framework.Pages;component/Assets/Images/reply.png", UriKind.Relative);
            UiTypeAction = WorkflowTypeAction.Reply;
            IsGlobalAction = true;
        }

        #endregion

        protected override void ActionClicked()
        {
            if (RulesAction != null)
            {
                _organizationId =
                    Int64.Parse(
                        RulesAction.ActionParameters.Where(p => p.Name == "OrganizationId").Select(p => p.Value).
                            SingleOrDefault());
            }

            Organization organization =
                GlobalWebContext.CurrentUser.GroupList.SelectMany(g => g.OrganizationList).Where(
                    o => o.Identifier == _organizationId).FirstOrDefault();

            if (RulesAction != null)
            {
                _folderTypeId = Int64.Parse(
                    RulesAction.ActionParameters.Where(p => p.Name == "FolderTypeId").Select(p => p.Value).
                        SingleOrDefault());
            }

            if (organization != null) {
                FolderType folderType =
                    organization.FolderTypesList.Where(ft => ft.Identifier == _folderTypeId).FirstOrDefault();

                AddDocumentChildWindow addDocumentChildWindow = new AddDocumentChildWindow(
                    folderType,
                    organization,
                    null,
                    Int32.Parse(GlobalWebContext.RioAppSettings["MaxNumberOfFilesImported"]))
                                                                    {
                                                                        Width =
                                                                            Application.Current.Host.Content.ActualWidth
                                                                            - 10,
                                                                        Height =
                                                                            Application.Current.Host.Content.ActualHeight
                                                                            - 10
                                                                    };

                addDocumentChildWindow.Closed += AddDocumentChildWindowClosed;
                addDocumentChildWindow.ShowDialog();
            }
        }

        private void AddDocumentChildWindowClosed(object sender, EventArgs e)
        {
            AddDocumentChildWindow addDocumentChildWindowSource = sender as AddDocumentChildWindow;
            if (addDocumentChildWindowSource != null)
            {
                addDocumentChildWindowSource.Closed -= AddDocumentChildWindowClosed;

                if (addDocumentChildWindowSource.DialogResult != null && addDocumentChildWindowSource.DialogResult == true)
                {

                    NavigationHelper.Instance.FolderLinkBuilded += Instance_FolderLinkBuilded;
                    NavigationHelper.Instance.GetFolderLink(_organizationId, _folderTypeId, addDocumentChildWindowSource.m_oControlImportPanel.FolderIdAfterIndexation, addDocumentChildWindowSource.m_oControlImportPanel.FolderIdDocAfterIndexation);
                }
            }
        }

        private void Instance_FolderLinkBuilded(object sender, NavigationEventArgs e)
        {
            NavigationHelper.Instance.FolderLinkBuilded -= Instance_FolderLinkBuilded;
            ExecuteAction(new List<Parameter>
                              {
                                  new Parameter
                                      {
                                          ParameterName = "COMMENT",
                                          ParameterValue = e.Uri.OriginalString
                                      }
                              });
        }

    }
}